package com.sj.purchase.manager.stages;

import com.sj.purchase.util.Date2StrUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.Date;
import java.util.List;

/**
 * @author yangrd
 * @date 2018/8/10
 **/
@Component
public class StagesOrderManager {

    /**
     * 订单分期接口
     */
    private static final String STAGES_ORDER_URL = "https://139.129.247.87:8086/order/all?startDate={1}&endDate={2}";

    private RestTemplate restTemplate;

    @Autowired
    public StagesOrderManager(@Qualifier("httpsRestTemplate") RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    public List<StagesOrder> listAll(Date startDay, Date endDay) {
        return restTemplate.exchange(STAGES_ORDER_URL, HttpMethod.GET, null, new ParameterizedTypeReference<List<StagesOrder>>() {
        }, format(startDay), format(endDay)).getBody();
    }

    private String format(Date date) {
        return Date2StrUtils.str(date).replaceAll("/", "-");
    }

}
